<template>
  <el-form v-bind="$attrs" @validate="formValidate">
    <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '名称不能为空' }]">
      <el-input v-model="formData.name" placeholder="请输入数据标题，不超过50个字符" />
    </el-form-item>
    <el-form-item label="描述" prop="desc">
      <el-input
        v-model="formData.desc"
        type="textarea"
        :rows="1"
        autosize
        @blur="specialFormValidate('desc')"
        @keyup.enter.native="specialFormValidate('desc')"
        placeholder="请输入数据的描述信息，不超过200个字符"
      />
    </el-form-item>
    <el-form-item label="填充">
      <el-color-picker
        v-model="formData.attr.fillColor"
        @change="specialFormValidate('fillColor')"
      />
      <div class="row-item-small">
        <el-input
          v-model="formData.attr.fillColor"
          placeholder=""
          @blur="specialFormValidate('fillColor')"
          @keyup.enter.native="specialFormValidate('fillColor')"
        />
      </div>
    </el-form-item>
    <el-form-item label="可视距离">
      <div class="row-item">
        <el-input
          v-model="convertObj.minDisplayDistance"
          controls-position="right"
          :min="0"
          @blur="specialFormValidate('minDisplayDistance')"
          @keyup.enter.native="specialFormValidate('minDisplayDistance')"
        />
        <div class="unit">米</div>
        <div class="unit">—</div>
        <el-input
          v-model="convertObj.maxDisplayDistance"
          controls-position="right"
          :min="0"
          @blur="specialFormValidate('maxDisplayDistance')"
          @keyup.enter.native="specialFormValidate('minDisplayDistance')"
        />
        <div class="unit">米</div>
      </div>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { messageUtil } from '@jl/element-plus/utils';

defineOptions({
  name: '',
});

interface IProps {
  form: any;
}

const props = withDefaults(defineProps<IProps>(), {
  form: {},
});

const formData = ref<any>(null);
const emits = defineEmits(['formValidate']);
const convertObj = ref<any>({
  minDisplayDistance: '0.00',
  maxDisplayDistance: '0.00',
});
function formValidate(fieldName: string, valid: boolean) {
  emits('formValidate', fieldName, valid);
}

function specialFormValidate(fieldName: string) {
  let valid = true;
  if (['desc'].includes(fieldName)) {
    if (formData.value[fieldName].length > 200) {
      valid = false;
    }
  }
  if (['fillColor'].includes(fieldName)) {
    if (!formData.value.attr[fieldName]) valid = false;
  }
  if (['width'].includes(fieldName)) {
    if (formData.value.attr[fieldName] === '' || isNaN(formData.value.attr[fieldName])) {
      valid = false;
    }
  }
  if (['minDisplayDistance', 'maxDisplayDistance'].includes(fieldName)) {
    if (convertObj.value[fieldName] === '' || isNaN(convertObj.value[fieldName])) {
      false;
    } else {
      formData.value.attr[fieldName] = +convertObj.value[fieldName];
    }
  }
  if (!valid) {
    messageUtil.warning('校验错误');
  }
  emits('formValidate', fieldName, valid);
}

watchEffect(() => {
  const { cimInstance } = window;
  const layer = props.form;
  const config = cimInstance.api.getCurrentLayerConfig(layer.layerName);
  console.log('-----config-----', config);
  layer.attr = {
    fillColor: config.fillColor || '#FF0000',
    minDisplayDistance: config.minDisplayDistance || 0,
    maxDisplayDistance: config.maxDisplayDistance,
  };
  convertObj.value.minDisplayDistance =
    (config.minDisplayDistance && config.minDisplayDistance.toFixed(2)) || '0.00';
  convertObj.value.maxDisplayDistance =
    config.maxDisplayDistance && config.maxDisplayDistance.toFixed(2);
  console.log('----layer-----', convertObj.value);
  formData.value = layer;
});
</script>

<style lang="less" scoped>
.el-form {
  .row-item-small {
    width: 100px;
    margin-left: 10px;
    line-height: normal;
  }

  .outline {
    width: 120px;
    margin-left: 28px;
    line-height: normal;
  }

  .line {
    height: 1px;
    background: #1f2329;
    width: 100%;
  }

  .row-item {
    width: 100%;
    display: flex;

    .el-input {
      flex: 1;
    }

    .unit {
      flex: 0 0 24px;
      text-align: center;
    }
  }
}
</style>
